package org.jwos.platform.domain;

public class ApplicationException extends Exception {
	
	public static final String ERROR = "ERROR";
	
	public static final String DELIMITER = ":";

	public ApplicationException() {
		super();
	}

	public ApplicationException(String msg) {
		super(msg);
	}
	
	public ApplicationException(String code,String msg) {		
		super(generateMessage(code,msg));
	}

	public ApplicationException(String msg, Throwable cause) {		
		super(msg, cause);
	}
	
	public ApplicationException(String code, String msg, Throwable cause) {		
		super(generateMessage(code,msg), cause);
	}

	public ApplicationException(Throwable cause) {
		super(cause);
	}
	
	private static String generateMessage(String code,String msg) {
		String[] errors = new String[3];
		
		errors[0] = ERROR;
		errors[1] = code;
		errors[2] = msg;
		
		return join(errors, DELIMITER);
	}
	
	private static String join(String[] errors,String delimiter) {
		String result = "";
		
		for(int i=0;i<errors.length;i++) {
			result += i>0 ? delimiter : "";
			result += errors[i];
		}
		
		return result;
	}	
}
